//
//  WYNetworkTool.m
//  WYNews
//
//  Created by HM09 on 17/3/19.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "WYNetworkTool.h"

@implementation WYNetworkTool

+ (instancetype) sharedTool {
    static WYNetworkTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[WYNetworkTool alloc] initWithBaseURL:nil];
    });
    
    return tool;
}

- (void) reqeustWith: (NSString *)url
              method: (NSString *)method
          parameters: (NSDictionary *) parameters
            callBack: (void(^)(id)) callBack {
    //调用AFN发起GET请求
    if ([method isEqualToString:@"GET"]) {
        [self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
            callBack(nil);
        }];
    }
    
    //调用AFN发起post请求
    if ([method isEqualToString:@"POST"]) {
        [self POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
            callBack(nil);
        }];
    }
}


@end
